// pages/shopping/shopping.js
var shihan = require('../../common/fetch')
var config = require('../../common/config')
var app = getApp()
var token;
var sceneres = {}
var phonepixel = 750 / app.data.deviceInfo.windowWidth;
Page({
  /* 页面的初始数据*/
  data: {
    token: '',
    txtStyle: '',
    isoffline: true,
    shopplist: '',
    allnumber: '0',
    allmoney: '0',
    shopplistGoods: [],
    selectproductid: [],
    toast_flag: false,
    goregister: true,
    delBtnWidth: '',
    dialogflag: true,
    showLoading: true,
    shopscrollflag: true,
    selectallstatus: false,
    is_refresh: true
  },
  onLoad: function(options) {
    var that = this
    var res = wx.getSystemInfoSync()
    this.setData({
      winWidth: res.windowWidth,
      delBtnWidth: 276 / phonepixel
    })
    app.wechatSwitch().then(function(wechat_switch) { //是否打开微信授权开关
      app.commonLogin(sceneres, wechat_switch)
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this, token = wx.getStorageSync('token');

    that.checknetword(that.data.offoptions);
    wx.removeStorageSync('invoicekey');

    if (token) {
      that.setData({
        showLoading: true
      })
      if (that.data.is_refresh || app.data.is_refresh_shopping == 1) {
        var shopping_cache = wx.getStorageSync('shopping_pic_cache');
        if (shopping_cache) {
          that.setData({
            shopplist: shopping_cache
          })
        }
        that.refreshlist();
        app.data.is_refresh_shopping = 0;
      } else {
        that.setData({
          showLoading: false
        })
      }
      that.setData({
        usertoken: token
      })
    } else {
      that.setData({
        showLoading: false
      })
    }
  },
  goLogin: function() {
    app.commonNoLogin()
  },
  checknetword: function (options) {
    var that = this
    wx.getNetworkType({
      success: function (res) { // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
        // console.log(res)
        if (res.networkType == 'none') {
          that.setData({
            isoffline: false,
            offoptions: options || ''
          })
          wx.stopPullDownRefresh()
        } else {
          that.setData({
            isoffline: true
          })
        }
      }
    })
  },
  refreshdata: function() {
    this.setData({
      showLoading: true
    })
    this.onShow();
  },
  touchS: function(e) {
    this.slideclear()
    // console.log(e)
    if (e.touches.length == 1) {
      this.setData({
        startX: e.touches[0].clientX, //设置触摸起始点水平方向位置
        startY: e.touches[0].clientY
      });
    }
  },
  touchM: function(e) {
    var that = this,
      store = e.currentTarget.dataset.store,
      goods = e.currentTarget.dataset.goods;
    if (e.touches.length == 1 && !this.data.shopplist.sh_sellers[store].editorstatus) {
       
      var moveX = e.touches[0].clientX;//手指移动时水平方向位置
      var moveY = e.touches[0].clientY;
  
      var disX = this.data.startX - moveX;//手指起始点位置与移动期间的差值
      var disY = this.data.startY - moveY;
      var delBtnWidth = this.data.delBtnWidth;

      if (disX == 0 || disX < 0) { //如果移动距离小于等于0，文本层位置不变  silder
        that.data.shopplist.sh_sellers[store].sh_products[goods].txtStyle = "left:0px"
      } else if (disX > 0) { //移动距离大于0，文本层left值等于手指移动距离
        if (disX > Math.abs(disY)) {
          that.setData({
            shopscrollflag: false //禁止上下滑动
          })
        }
        that.data.shopplist.sh_sellers[store].sh_products[goods].txtStyle = "left:-" + disX + "px"
        that.data.shopplist.sh_sellers[store].sh_products[goods].silder = "silder"
        that.data.shopplist.sh_sellers[store].silder = "silder"
        if (disX >= delBtnWidth) {
          //控制手指移动距离最大值为删除按钮的宽度 
          that.setData({
            shopscrollflag: false //禁止上下滑动
          })
          that.data.shopplist.sh_sellers[store].sh_products[goods].txtStyle = "left:-" + delBtnWidth + "px"
          that.data.shopplist.sh_sellers[store].sh_products[goods].silder = "silder"
          that.data.shopplist.sh_sellers[store].silder = "silder"
        }
      }
      that.setData({
        shopplist: that.data.shopplist
      })
    }
  },
  touchE: function(e) {
    var that = this
    var store = e.currentTarget.dataset.store
    var goods = e.currentTarget.dataset.goods
    if (e.changedTouches.length == 1 && !this.data.shopplist.sh_sellers[store].editorstatus) {
      //手指移动结束后水平位置  
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离  
      var disX = this.data.startX - endX;
      var delBtnWidth = this.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮  
      this.data.shopplist.sh_sellers[store].sh_products[goods].txtStyle = disX > delBtnWidth / 5 ? "left:-" + delBtnWidth + "px" : "left:0px"
      if (disX > delBtnWidth / 5) {
        this.data.shopplist.sh_sellers[store].sh_products[goods].silder = "silder"
        this.data.shopplist.sh_sellers[store].silder = "silder"
      }
      this.setData({
        shopplist: this.data.shopplist
      })
      setTimeout(function() {
        that.setData({
          shopscrollflag: true
        })
      }, 400)
    }
  },
  onshopScroll: function(ev) {
    this.slideclear()
    // console.log(ev.detail.scrollTop)
  },
  lookplatform: function(e) {
    var adverttype = e.currentTarget.dataset.type
    var advertid = e.currentTarget.dataset.id
    if (adverttype&&adverttype.indexOf('https://') == 0) {
      wx.navigateTo({
        url: '/pages/strategyinfo/strategyinfo?share=0&nologin=1&url=' + encodeURIComponent(adverttype)
      })
    }
  },
  recomgoods: function() {
    var that = this
    shihan.sendRequest(config.get_cart_list, 'POST', {})
      .then(function(response) {
        if (response.data.code == 0) {
          for (var i = 0; i < response.data.data.sh_rem_products.length; i++) {
            if (response.data.data.sh_rem_products[i].sh_label.sh_location) {
              var pos = response.data.data.sh_rem_products[i].sh_label
              pos['posa'] = pos.sh_location.split('_')[0];
              pos['posb'] = pos.sh_location.split('_')[1];
            }
          }
          that.setData({
            shopplistGoods: response.data.data.sh_rem_products
          })
          if (response.data.data.sh_adv && response.data.data.sh_adv.length > 0) {
            that.setData({
              topadv: response.data.data.sh_adv[0]
            })
          }
        }
      }, function(error) {
        console.log(error);
      });
  },
  btneditor: function(e) {
    var that = this
    this.slideclear()
    var index = e.currentTarget.dataset.index
    var store = e.currentTarget.dataset.store
    var silder = this.data.shopplist.sh_sellers[store].silder
    if (silder) {
      this.data.shopplist.sh_sellers[store].silder = ''
      setTimeout(function() {
        if (that.data.shopplist.sh_sellers[index].editorstatus) {
          that.data.shopplist.sh_sellers[index].editorstatus = false
          that.data.shopplist.sh_sellers[index].editorword = '编辑'
        } else {
          that.data.shopplist.sh_sellers[index].editorstatus = true
          that.data.shopplist.sh_sellers[index].editorword = '完成'
        }
        that.setData({
          shopplist: that.data.shopplist
        })
      }, 400)
    } else {
      if (this.data.shopplist.sh_sellers[index].editorstatus) {
        this.data.shopplist.sh_sellers[index].editorstatus = false
        this.data.shopplist.sh_sellers[index].editorword = '编辑'
      } else {
        this.data.shopplist.sh_sellers[index].editorstatus = true
        this.data.shopplist.sh_sellers[index].editorword = '完成'
      }
    }
    this.setData({
      shopplist: this.data.shopplist
    })
  },
  goodsgoshop: function(e) {
    if (e.currentTarget.dataset.store>=0) {
      this.slideclear()
      var store = e.currentTarget.dataset.store
      var goods = e.currentTarget.dataset.goods
      var silder = this.data.shopplist.sh_sellers[store].sh_products[goods].silder
      if (silder) { //left:0px
        this.data.shopplist.sh_sellers[store].sh_products[goods].silder = ""
        this.data.shopplist.sh_sellers[store].sh_products[goods].txtStyle = "left:0px"
        this.setData({
          shopplist: this.data.shopplist
        })
      } else {
        wx.navigateTo({
          url: '/pages/goodsdetail/goodsdetail?goodsid=' + e.currentTarget.dataset.id
        })
      }
    } else {
      wx.navigateTo({
        url: '/pages/goodsdetail/goodsdetail?goodsid=' + e.currentTarget.dataset.id
      })
    }
  },
  slideclear: function() {
    this.data.shopplist.sh_sellers.forEach(function(el, index) {
      el.sh_products.forEach(function(ele, index) {
        ele.txtStyle = "left:0px"
      })
    })
    this.setData({
      shopplist: this.data.shopplist
    })
  },
  selectstore: function(e) {
    var index = e.currentTarget.dataset.index
    var sh_sellers = this.data.shopplist.sh_sellers
    if (sh_sellers[index].noselectflag == true) {
      if (sh_sellers[index].selectstatus == false) {
        sh_sellers[index].selectstatus = true
        for (var i = 0; i < sh_sellers[index].sh_products.length; i++) {
          sh_sellers[index].sh_products[i].selectstatus = true
        }
      } else {
        sh_sellers[index].selectstatus = false
        for (var i = 0; i < sh_sellers[index].sh_products.length; i++) {
          sh_sellers[index].sh_products[i].selectstatus = false
        }
      }
    }
    // 判断全选是否亮起
    var selectallflag = true
    var sh_sellers = this.data.shopplist.sh_sellers
    for (var i = 0; i < sh_sellers.length; i++) {
      if (sh_sellers[i].selectstatus == false) {
        selectallflag = false
      }
    }
    if (selectallflag) {
      this.setData({
        selectallstatus: true
      })
    } else {
      this.setData({
        selectallstatus: false
      })
    }
    this.setData({
      shopplist: this.data.shopplist
    })
    this.selectgoodsid()
    // console.log(this.data.selectproductid)
  },
  selectgoods: function(e) {
    var store = e.currentTarget.dataset.store
    var goods = e.currentTarget.dataset.goods
    var sh_sellers = this.data.shopplist.sh_sellers
    var selectflag = true
    if (sh_sellers[store].sh_products[goods].sh_buy_qty <= sh_sellers[store].sh_products[goods].sh_stock) {
      if (sh_sellers[store].sh_products[goods].selectstatus == false) {
        sh_sellers[store].sh_products[goods].selectstatus = true
      } else {
        sh_sellers[store].sh_products[goods].selectstatus = false
      }
    }
    for (var i = 0; i < sh_sellers[store].sh_products.length; i++) {
      if (sh_sellers[store].sh_products[i].sh_buy_qty <= sh_sellers[store].sh_products[i].sh_stock) {
        if (sh_sellers[store].sh_products[i].selectstatus == false) {
          selectflag = false
        }
      }
    }
    if (selectflag) {
      sh_sellers[store].selectstatus = true
    } else {
      sh_sellers[store].selectstatus = false
    }
    // 判断全选是否亮起
    var selectallflag = true
    var sh_sellers = this.data.shopplist.sh_sellers
    for (var i = 0; i < sh_sellers.length; i++) {
      if (sh_sellers[i].selectstatus == false) {
        selectallflag = false
      }
    }
    if (selectallflag) {
      this.setData({
        selectallstatus: true
      })
    } else {
      this.setData({
        selectallstatus: false
      })
    }
    this.setData({
      shopplist: this.data.shopplist
    })
    this.selectgoodsid()
    // console.log(this.data.selectproductid)
  },
  selectallgoods: function() {
    var sh_sellers = this.data.shopplist.sh_sellers
    if (sh_sellers) {
      if (this.data.selectallstatus == false) {
        this.setData({
          selectallstatus: true
        })
        for (var i = 0; i < sh_sellers.length; i++) {
          for (var j = 0; j < sh_sellers[i].sh_products.length; j++) {
            sh_sellers[i].selectstatus = true
            sh_sellers[i].sh_products[j].selectstatus = true
            this.data.selectproductid.push(sh_sellers[i].sh_products[j].sh_id)
          }
        }
      } else {
        this.setData({
          selectallstatus: false
        })
        for (var i = 0; i < sh_sellers.length; i++) {
          for (var j = 0; j < sh_sellers[i].sh_products.length; j++) {
            sh_sellers[i].selectstatus = false
            sh_sellers[i].sh_products[j].selectstatus = false
            this.data.selectproductid.length = 0
          }
        }
      }
      this.setData({
        shopplist: this.data.shopplist
      })
      this.selectgoodsid()
    }
  },
  selectmoney: function(store, goods) {
    var that = this
    shihan.sendRequest(config.get_cart_list, 'POST', {
        product_id: this.data.selectproductid
      })
      .then(function(response) {
        // console.log(response);
        if (store >= 0 & goods >= 0) {
          var sh_sellers = that.data.shopplist.sh_sellers
          if (response.data.data.sh_sellers[store].sh_products[goods].sh_error_tips) {
            sh_sellers[store].sh_products[goods].sh_error_tips = response.data.data.sh_sellers[store].sh_products[goods].sh_error_tips
          } else {
            sh_sellers[store].sh_products[goods].sh_error_tips = ''
          }
        }
        that.setData({
          shopplist: that.data.shopplist,
          allmoney: response.data.data.sh_products_price_total || '0',
          allnumber: response.data.data.sh_product_count || ''
        })
      }, function(error) {
        console.log(error);
      });
    // console.log(that.data);
  },
  selectgoodsid: function() {
    var sh_sellers = this.data.shopplist.sh_sellers
    this.data.selectproductid.length = 0
    for (var i = 0; i < sh_sellers.length; i++) {
      for (var j = 0; j < sh_sellers[i].sh_products.length; j++) {
        if (sh_sellers[i].sh_products[j].sh_buy_qty <= sh_sellers[i].sh_products[j].sh_stock) {
          if (sh_sellers[i].sh_products[j].selectstatus == true) {
            this.data.selectproductid.push(sh_sellers[i].sh_products[j].sh_id)
          }
        }
      }
    }
    this.setData({
      selectproductid: this.data.selectproductid
    })
    if (this.data.selectproductid.length > 0) {
      this.selectmoney()
    } else {
      this.setData({
        allnumber: '0',
        allmoney: '0'
      })
    }
  },
  btnminutes: function(e) {
    var store = e.currentTarget.dataset.store,
      goods = e.currentTarget.dataset.goods,
      sh_sellers = this.data.shopplist.sh_sellers,
      id = sh_sellers[store].sh_products[goods].sh_id,
      buy_qty = Number(sh_sellers[store].sh_products[goods].sh_buy_qty);

    buy_qty--
    if (buy_qty < 1) {
      sh_sellers[store].sh_products[goods].numjiantxt = ''
      if (sh_sellers[store].sh_products[goods].sh_stock > 0) {
        shihan.showToast.call(this, '亲，请至少购买一件哦！');
      }
      buy_qty = 1
    } else {
      sh_sellers[store].sh_products[goods].numjiantxt = 2
      if (buy_qty < sh_sellers[store].sh_products[goods].sh_stock) {
        sh_sellers[store].sh_products[goods].numjiatxt = 2
      }
    }
    this.changegoodsnum(id, buy_qty, store, goods);
  },
  btnplus: function(e) {
    var stock = e.currentTarget.dataset.stock,
      store = e.currentTarget.dataset.store,
      goods = e.currentTarget.dataset.goods,
      sh_sellers = this.data.shopplist.sh_sellers,
      id = sh_sellers[store].sh_products[goods].sh_id,
      buy_qty = Number(sh_sellers[store].sh_products[goods].sh_buy_qty);

    buy_qty++
    if (buy_qty > sh_sellers[store].sh_products[goods].sh_stock) {
      if (sh_sellers[store].sh_products[goods].sh_stock == 0) {
        buy_qty = 1
      } else {
        buy_qty = sh_sellers[store].sh_products[goods].sh_stock
      }
      sh_sellers[store].sh_products[goods].numjiatxt = ''
      if (sh_sellers[store].sh_products[goods].sh_stock > 0) {
        shihan.showToast.call(this, '亲，库存已达上限哦');
      }
    } else {
      sh_sellers[store].sh_products[goods].numjiatxt = 2
      if (buy_qty > 1) {
        sh_sellers[store].sh_products[goods].numjiantxt = 2
      }
    }
    this.changegoodsnum(id, buy_qty, store, goods);
  },
  changegoodsnum: function(id, num, store, goods) {
    var that = this, carts = {};
    carts[id] = num;
    shihan.sendRequest(config.update_cart_item, 'POST', { carts: JSON.stringify(carts) })
      .then(function(response) {
        if (response.data.code == 0) {
          var sh_sellers = that.data.shopplist.sh_sellers
          sh_sellers[store].sh_products[goods].sh_buy_qty = response.data.data.sh_product.sh_qty
          sh_sellers[store].sh_products[goods].prosignflag = response.data.data.sh_product.sh_msg
          sh_sellers[store].noselectflag = true
          if (response.data.data.sh_product.sh_msg) {
            that.setData({
              prosignflag: false
            })

            shihan.showToast.call(that, response.data.data.sh_product.sh_msg);
          }
          that.setData({
            shopplist: that.data.shopplist
          })
        } else {
          that.setData({
            numjiantxt: ''
          })
        }
        that.selectmoney(store, goods)
        // that.refreshlist()
      }, function(error) {
        console.log(error);
      });
  },
  btnpayfor: function(e) {
    var that = this,
      selectproduct = [],
      sh_sellers = this.data.shopplist.sh_sellers;

    for (var i = 0; i < sh_sellers.length; i++) {
      for (var j = 0; j < sh_sellers[i].sh_products.length; j++) {
        if (sh_sellers[i].sh_products[j].sh_buy_qty <= sh_sellers[i].sh_products[j].sh_stock) {
          if (sh_sellers[i].sh_products[j].selectstatus == true) {
            selectproduct.push(sh_sellers[i].sh_products[j].sh_id)
          }
        }
      }
    }
    if (selectproduct.length > 0) {
      shihan.sendRequest(config.confirm_order, 'POST', {
          product_id: selectproduct
        })
        .then(function(response) {
          if (response.data.code == 0) {
            wx.navigateTo({
              url: '/pages/firmorder/firmorder?buytype=shopping&need=1&product_id=' + selectproduct
            })
          } else if (response.data.code == 1101) { //用户未登录，清除token
            wx.setStorageSync('token', '');
            app.commonNoLogin()
          } else {
            shihan.showToast.call(that, response.data.msg);
          }
        }, function(error) {
          console.log(error);
        });

    } else {
      shihan.showToast.call(that, '亲，请至少选择一件商品哦！');
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  refreshlist: function() {
    var that = this;
    shihan.sendRequest(config.get_cart_list, 'POST', {})
      .then(function(response) {
        if (response.data.code == 0) {
          if (response.data.data.sh_sellers) {
            for (var i = 0; i < response.data.data.sh_sellers.length; i++) {
              response.data.data.sh_sellers[i].editorword = '编辑';
              response.data.data.sh_sellers[i].editorstatus = false;
              response.data.data.sh_sellers[i].selectstatus = false;
              response.data.data.sh_sellers[i].silder = '';
              for (var j = 0; j < response.data.data.sh_sellers[i].sh_products.length; j++) {
                response.data.data.sh_sellers[i].sh_products[j].selectstatus = false;
                response.data.data.sh_sellers[i].sh_products[j].prosignflag = false;
                response.data.data.sh_sellers[i].sh_products[j].numjiantxt = '';
                response.data.data.sh_sellers[i].sh_products[j].numjiatxt = '';
                response.data.data.sh_sellers[i].sh_products[j].txtStyle = '';
                response.data.data.sh_sellers[i].sh_products[j].silder = '';
                var a = response.data.data.sh_sellers[i].sh_products[j]
                if (a.sh_buy_qty == 1) {
                  if (a.sh_stock > a.sh_buy_qty) {
                    a.numjiantxt = '';
                    a.numjiatxt = 2;
                  } else if (a.sh_stock <= a.sh_buy_qty) {
                    a.numjiantxt = '';
                    a.numjiatxt = '';
                  }
                } else if (a.sh_buy_qty > 1) {
                  if (a.sh_stock > a.sh_buy_qty) {
                    a.numjiantxt = 2;
                    a.numjiatxt = 2;
                  } else if (a.sh_stock <= a.sh_buy_qty) {
                    a.numjiantxt = 2;
                    a.numjiatxt = '';
                  } else if (a.sh_stock == 0) {
                    a.numjiantxt = '';
                    a.numjiatxt = '';
                  }
                }
              }
            }
          }
          if (response.data.data.sh_rem_products && response.data.data.sh_rem_products.length > 0) {
            for (var i = 0; i < response.data.data.sh_rem_products.length; i++) {
              if (response.data.data.sh_rem_products[i].sh_label.sh_location) {
                var pos = response.data.data.sh_rem_products[i].sh_label
                pos['posa'] = pos.sh_location.split('_')[0];
                pos['posb'] = pos.sh_location.split('_')[1];
              }
            }
            that.setData({
              shopplistGoods: response.data.data.sh_rem_products
            })
          }
          if (response.data.data.sh_adv && response.data.data.sh_adv.length > 0) {
            that.setData({
              topadv: response.data.data.sh_adv[0]
            })
          }
          that.setData({
            shopplist: response.data.data,
            allnumber: '0',
            allmoney: '0',
            selectallstatus: false
          })
          var title = response.data.args.cart_num > 0 ? '购物车(' + response.data.args.cart_num + ')' : '购物车'
          wx.setNavigationBarTitle({
            title: title,
          })
        } else {
          if (response.data.code == 1101) { //用户未登录，清除token
            wx.setStorageSync('token', '');
            app.commonNoLogin()
          } else if (response.data.code == 1112) {
            if (that.data.shopplist) {
              that.data.shopplist.sh_sellers.length = 0
              that.setData({
                shopplistGoods: [],
                shopplist: that.data.shopplist
              })
            }
            shihan.showToast.call(that, response.data.msg);
          }
        }
        that.noselect()
        that.setData({
          showLoading: false
        })
        wx.setStorageSync('shopping_pic_cache', that.data.shopplist);
      }, function(error) {
        console.log(error);
      });
  },
  viewgoodsDetail: function(e) {
    wx.navigateTo({
      url: '/pages/goodsdetail/goodsdetail?goodsid=' + e.currentTarget.dataset.id
    })
  },
  storegoshop: function(e) {
    wx.navigateTo({
      url: '/pages/storedetail/storedetail?id=' + e.currentTarget.dataset.id
    })
  },
  deletegoods: function(e) { //remove_to_cart
    var that = this
    wx.showModal({
      title: "提示",
      content: '确定要删除改商品吗',
      confirmText: "确认",
      confirmColor: "#c40000",
      showCancel: true,
      success: function(res) {
        if (res.confirm) {
          that.setData({
            showLoading: true
          })
          shihan.sendRequest(config.remove_to_cart, 'POST', {
              product_ids: e.currentTarget.dataset.id
            })
            .then(function(response) {
              console.log(response);
              if (response.data.code == 0) {
                that.refreshlist();
                shihan.showToast.call(that, '删除商品成功');
              }
            }, function(error) {
              console.log(error);
            });
        }
      }
    })
  },
  addcollection: function(e) {
    var that = this,
      collecid = e.currentTarget.dataset.id,
      goodsid = e.currentTarget.dataset.goodsid;

    shihan.sendRequest(config.add_favorite, 'POST', {
        id: collecid,
        type: 1
      })
      .then(function(response) {
        if (response.data.code == 0 || response.data.code == 1103) {
          shihan.showToast.call(that, '收藏成功');
          shihan.sendRequest(config.remove_to_cart, 'POST', {
              product_ids: goodsid
            })
            .then(function(response) {
              console.log(response);
              if (response.data.code == 0) {
                that.refreshlist()
              }
            }, function(error) {
              console.log(error);
            });
        } else {
          shihan.showToast.call(that, response.data.msg);
        }
      }, function(error) {
        console.log(error);
      });
  },
  productdelete: function() {
    var that = this,
      arra = [],
      productlist = this.data.shopplist;

    for (var i = 0; i < productlist.sh_sale_off.length; i++) {
      arra.push(productlist.sh_sale_off[i].sh_id);
    }
    wx.showModal({
      title: "提示",
      content: '确定要清空失效商品吗',
      confirmText: "确认",
      confirmColor: "#c40000",
      showCancel: true,
      success: function(res) {
        if (res.confirm) {
          that.setData({
            showLoading: true
          })
          shihan.sendRequest(config.remove_to_cart, 'POST', {
              product_ids: arra.join(',')
            })
            .then(function(response) {

              if (response.data.code == 0) {
                productlist.sh_sale_off.length = 0;
                that.setData({
                  showLoading: false,
                  shopplist: that.data.shopplist
                })

                shihan.showToast.call(that, '清空失效商品成功');
              } else {
                shihan.showToast.call(that, response.data.msg);
              }
            }, function(error) {
              console.log(error);
            });
          setTimeout(function() {
            that.setData({
              showLoading: false
            })
          }, 300)
        }
      }
    })
  },
  noselect: function() {
    var flag;
    if (this.data.shopplist) {
      var sh_sellers = this.data.shopplist.sh_sellers
      if (sh_sellers) {
        for (var i = 0; i < sh_sellers.length; i++) {
          flag = true
          sh_sellers[i].noselectflag = true
          for (var j = 0; j < sh_sellers[i].sh_products.length; j++) {
            // if (sh_sellers[i].sh_products[j].sh_stock > 0) {
            //   flag = false
            // }
            if (sh_sellers[i].sh_products[j].sh_buy_qty <= sh_sellers[i].sh_products[j].sh_stock) {
              flag = false
            }
          }
          if (flag) {
            sh_sellers[i].noselectflag = false
          }
        }
        this.setData({
          shopplist: this.data.shopplist
        })
      }
    }
  },
  fixedpos: function() {
    this.setData({
      fixedtopflag: true
    })
  },
  onScroll: function(ev) {
    var scrollTop = ev.detail.scrollTop;
    this.setData({
      scrolltop: ev.detail.scrollTop
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      goregister: true,
      is_refresh: true
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    this.setData({
      goregister: true
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  }
})